//
//  AppDelegate.m
//  MXLoginMethodsDomo
//
//  Created by 马彦 on 16/9/26.
//  Copyright © 2016年 MX. All rights reserved.
//


#import "MXUserModel.h"

#import "AppDelegate.h"
#import "ViewController.h"
#import "MXLoginViewController.h"
#import "SuperID.h"
//#import "MXFaceRecgnitionModel.h"

#import "MXLockViewController.h"
#import "MXLockPassword.h"

#import "WJTouchID.h"
#import "MXTouchIDPassword.h"


#define SIDAPPID @"f2710948fc8af8d42c9c37bd"
#define SIDAPPSECRET @"a0d8e5b5540098376d39eba1"

@interface AppDelegate ()<MXLockViewControlerDelegate,WJTouchIDDelegate>

@end

@implementation AppDelegate

- (void)faceLogin {
    [[SuperID sharedInstance]registerAppWithAppID:SIDAPPID withAppSecret:SIDAPPSECRET];
    //开启SuperID SDK的调试模式，开发者在Release时，将该模式设置为NO.
    [SuperID setDebugMode:YES];
    //设置一登 SDK 的语言模式，默认为自动模式。
    [SuperID setLanguageMode:SIDAutoMode];
}

/**密码测试*/
- (void)testPassward {
    
    //密码设置
    if ([MXUserModel readUserName].length == 0 || [MXUserModel readPassword].length == 0) {
        [MXUserModel userNameSave:@"Mayan"];
        [MXUserModel passwordSave:@"123"];
        NSLog(@"账户和密码已经保存");
    } else {
        NSLog(@"-----------------------");
        NSLog(@"账号：%@ \n 密码:%@",[MXUserModel readUserName],[MXUserModel readPassword]);
        NSLog(@"-----------------------");
    }
    
}

- (void)LockOrTouchIDLogin{
    
    //指纹手势密码登录
    if (![MXLockPassword isEmptyPassword]) {
        MXLockViewController *lockVC = [[MXLockViewController alloc] initWithType:MXLockTypeVerify appearMode:MXLockAppearModePresent];
        lockVC.delegate = self;
        self.window.rootViewController = lockVC;
        [self.window makeKeyAndVisible];
    } else if (![MXTouchIDPassword isEmptyPassword]) {
        WJTouchID *touchID = [[WJTouchID alloc] init];
        [touchID  startWJTouchIDWithMessage:WJNotice(@"通过Home键验证已有手机指纹", @"The existing mobile phone fingerprints are verified through the Home button") fallbackTitle:WJNotice(@"验证登录密码", @"Verify password") delegate:self];
        
        self.window.rootViewController = [[ViewController alloc] init];
        [self.window makeKeyAndVisible];
    } else {
        MXLoginViewController *loginVC = [[MXLoginViewController alloc] init];
        
        self.window.rootViewController = loginVC;
        [self.window makeKeyAndVisible];
    }
}



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    UINavigationBar *appBar = [UINavigationBar appearance];
    appBar.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
    //脸部识别登录
    [self faceLogin];
    //测试账号密码
    [self testPassward];
    
    [self LockOrTouchIDLogin];

    return YES;
}

- (void)viewControllerView {
    ViewController *VC = [[ViewController alloc] init];
    UINavigationController *navVC = [[UINavigationController alloc] initWithRootViewController:VC];
    self.window.rootViewController = navVC;
    [self.window makeKeyAndVisible];
}

#pragma mark - MXLockViewControlerDelegate
/**验证手势登录*/
- (void)passwordDidVerify:(NSString *)oldPassword {
    [self viewControllerView];
}

#pragma mark - WJTouchIDDelegate
/**验证成功*/
- (void)WJTouchIDAuthorizeSuccess {
    [self viewControllerView];
}

/**验证失败*/
- (void)WJTouchIDAuthorizeFailure {
    
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}



@end
